package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.UserQuestionnaire;
import com.hkbigdata.sybx.mapper.UserQuestionnaireMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangjing
 * @creat 2025-05-14-21:49
 * @email 3010335359@qq.com
 */



@RestController
@RequestMapping("/api/userQuestionnaire")
public class UserQuestionnaireController {
    @Autowired
    private UserQuestionnaireMapper userQuestionnaireMapper;
    //查看所有用户信息
    @GetMapping("/all")
    public List<UserQuestionnaire> getAllUsers() {
        return userQuestionnaireMapper.getAllUsers();
    }
    //    增加信息
    @PostMapping("/insert")
    public String insertQuestionnaire(@RequestBody UserQuestionnaire userQuestionnaire) {
        int result = userQuestionnaireMapper.addQuestionnaire(userQuestionnaire);
        return result > 0 ? "提交成功" : "提交失败";
    }
    // 根据用户ID删除
    @DeleteMapping("/deleteByUser/{userId}")
    public Map<String, Object> deleteByUserId(@PathVariable("userId") String userId) {
        Map<String, Object> response = new HashMap<>();
        try {
            int affectedRows = userQuestionnaireMapper.deleteByUserId(userId);
            response.put("success", affectedRows > 0);
            response.put("message", affectedRows > 0 ? "删除成功" : "未找到该用户的问卷记录");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "删除异常：" + e.getMessage());
            e.printStackTrace();
        }
        return response;
    }
}